import { inject, observer } from 'mobx-react';
import { WeaTools, WeaProgress, WeaDialog } from 'ecCom';
import { Button } from 'antd';
import BaseList from '../BaseList';
import ReportDetail from './ReportDetail';
import * as Bctt from '../../constants/BaseListConstant';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

@inject('contractReportStore')
@observer
class Report extends BaseList {
  getStore() {
    return this.props.contractReportStore;
  }

  reRenderColumns(columns) {
    const store = this.getStore();
    const { type } = store.routerParams;
    if (type != 'report' && type != 'reportFna' && type != 'reportProduct') {
      columns.forEach((c) => {
        if (c.dataIndex == 'resultcount') {
          // 占比
          c.render = function (text, record) {
            if (!record.hasOwnProperty('_stat')) {
              return <span className="wea-crmReport-progressStyle"><WeaProgress ecId={`${this && this.props && this.props.ecId || ''}_WeaProgress@xijcim`} percent={record.resultcountspan} /></span>;
            }
            return <div />;
          };
        }
        if (type == 'reportAreaAmount' || type == 'reportCredit') {
          if (c.dataIndex == 'customercount') {
            // 客户计数
            c.render = function (text, record) {
              if (!record.hasOwnProperty('_stat')) {
                const params = { ...store.searchConditionForm.getFormParams() };
                // console.log(params);
                let url;
                if (type == 'reportAreaAmount') {
                  const manager = params.customerManager;
                  const contract_startDate_selectType = params.startDate_selectType;
                  const contract_startDate_fromDate = params.startDate_fromDate == null ? '' : params.startDate_fromDate;
                  const contract_startDate_toDate = params.startDate_toDate == null ? '' : params.startDate_toDate;
                  url = `/spa/crm/static/index.html#/main/crm/customer/report?province=${record.provinceid}&manager=${manager}&sellAreaResult=1&contract_startDate_selectType=${contract_startDate_selectType}&contract_startDate_fromDate=${contract_startDate_fromDate}&contract_startDate_toDate=${contract_startDate_toDate}`;
                } else if (type == 'reportCredit') {
                  const manager = params.customerManager;
                  const crmId = params.customer;
                  const datetype_selectType = params.startDate_selectType;
                  const datetype_fromDate = params.startDate_fromDate == null ? '' : params.startDate_fromDate;
                  const datetype_toDate = params.startDate_toDate == null ? '' : params.startDate_toDate;
                  url = `/spa/crm/static/index.html#/main/crm/customer/report?creditlevel=${record.randomFieldId}&manager=${manager}&crmId=${crmId}&datetype_selectType=${datetype_selectType}&datetype_fromDate=${datetype_fromDate}&datetype_toDate=${datetype_toDate}`;
                }
                // console.log(url)
                return <span onClick={() => { store.showCustomersDialog(url); }} ><a>{record[c.dataIndex]}</a></span>;
              }
              return <span>{record.customercount}</span>;
            };
          }
        } else if (type == 'reportCustomerAmount') {
          if (c.dataIndex == 'resultcount_n') {
            if (type != 'reportSumAmount') {
              // 计数
              c.render = function (text, record) {
                let keyWord = '';
                let keyWordValue = '';
                if (type == 'reportCustomerAmount') {
                  keyWord = 'customer';
                  keyWordValue = record.crmid;
                }
                if (!record.hasOwnProperty('_stat')) {
                  const params = { ...store.searchConditionForm.getFormParams() };
                  for (const item in params) {
                    if (params[item] == '') {
                      delete params[item];
                    }
                  }
                  params.type = 'report';
                  if (type == 'reportCustomerAmount') {
                    params.status = '2+';// 合同状态>=2
                  }
                  params[keyWord] = keyWordValue;
                  return <a onClick={() => { store.showContractDialog(true, params); }}>{record.resultcount_n}</a>;
                }
                return <span>{record.resultcount_n}</span>;
              };
            }
          }
        }
      });
      return columns;
    }
  }

  getTabSearchTypes(store) {
    const type = store.routerParams.type;
    const arr = [];
    arr.push(Bctt.TAB_SEARCH_TYPE_ADVANCED);
    if (type == 'report') {
      arr.push(Bctt.TAB_SEARCH_TYPE_BASE);
    }
    return arr;
  }

  renderAppend(store) {
    const type = store.routerParams.type;
    const arr = [];
    if (type == 'reportCustomerAmount') {
      const { contractDialogTile, contractDialogShow, showContractDialog, contractDialogParams } = store;
      if (contractDialogShow) {
        arr.push(<WeaDialog
          ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@s11gmx@dialog`}
          icon="icon-coms-crm"
          iconBgcolor="#96358a"
          style={{ width: 1000, height: 500 }}
          visible={contractDialogShow}
          title={contractDialogTile}
          buttons={[
            <Button
              ecId={`${this && this.props && this.props.ecId || ''}_Button@wa9w7l@close`}
              onClick={() => {
                showContractDialog(false, contractDialogParams);
              }}
            >{getLabel(309, '关闭')}</Button>,
          ]}
          onCancel={() => {
            showContractDialog(false, contractDialogParams);
          }}
        >
          <ReportDetail ecId={`${this && this.props && this.props.ecId || ''}_ReportDetail@ygh3gd@rm`} params={contractDialogParams} />
        </WeaDialog>);
      }
    }
    if (type == 'reportAreaAmount' || type == 'reportCredit') {
      const { title, visible, url, style } = store.dialog;
      if (visible) {
        arr.push(<WeaDialog
          ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@nzjwjv@dialog`}
          icon="icon-coms-crm"
          iconBgcolor="#96358a"
          title={title}
          visible={visible}
          url={url}
          onCancel={store.hideCustomersDialog}
          style={style}
          buttons={[<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@52zcr8@close`} type="primary" onClick={store.hideCustomersDialog}>{getLabel(309, '关闭')}</Button>]}
        />);
      }
    }
    return arr;
  }
}

export default WeaTools.tryCatch(React, props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@imlof9`} msg={props.error ? props.error : getLabel(129417, '服务器运行出错，请联系系统管理员！')} />, { error: '' })(Report);
